package com.woyowodaows.activity.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.woyowodaows.R;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.base.BaseBean;
import com.woyowodaows.bean.home.RegisterDataBean;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.ActivityManagerUtils;
import com.woyowodaows.utils.DisplayUtils;
import com.woyowodaows.utils.RegexpUtils;
import com.woyowodaows.utils.RegularUtils;
import com.woyowodaows.utils.ToastUtil;
import com.woyowodaows.weight.CountdownButton;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zys on 2018/7/26.
 * <p>
 * 注册页面
 */

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.et_registere_nickname)
    EditText etRegistereNickname;
    @BindView(R.id.et_registere_pwd)
    EditText etRegisterePwd;
    @BindView(R.id.et_registere_again_pwd)
    EditText etRegistereAgainPwd;
    @BindView(R.id.iv_registere_clear)
    ImageView ivRegistereClear;
    @BindView(R.id.et_registere_phone)
    EditText etRegisterePhone;
    @BindView(R.id.et_registere_verification_code)
    EditText etRegistereVerificationCode;
    @BindView(R.id.btn_verification_code)
    CountdownButton btnVerificationCode;
    @BindView(R.id.ck_register_set)
    CheckBox ckRegisterSet;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.et_registere_email)
    EditText etRegistereEmail;//邮箱
    @BindView(R.id.et_register_nick_name)
    EditText etRegisterNickName;//昵称
    @BindView(R.id.tv_agreement)
    TextView tvDeclare;
    @BindView(R.id.et_register_user_name)
    EditText etRegistereUserName;
    @BindView(R.id.tv_register_protocol)
    TextView tvRegisterProtocol;
    @BindView(R.id.ll_register_protocol)
    LinearLayout llRegisterProtocol;
    @BindView(R.id.iv_look_passwords)
    ImageView ivLookPasswords;


    private boolean isLookPassword;//是否可以查看密码
    private String messageId;//短信验证码标识

    private String recommendedPersonPhone;

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitleBar(false, "注册", "", 0, 0, null);
        getWindow().setSoftInputMode(   WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        ActivityManagerUtils.getInstance().getActivity(RegisterActivity.class);
        DisplayUtils.addLayoutListener(rlLayout, btnRegister);

    }


    @OnClick({R.id.iv_registere_clear, R.id.btn_verification_code,
            R.id.btn_register,
            R.id.tv_agreement,
            R.id.iv_look_passwords})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_registere_clear:
                break;
            case R.id.btn_verification_code://发送验证码
                getMessageCode();
                break;
            case R.id.btn_register://注册
                register();
                break;
            case R.id.iv_look_passwords:
                if (isLookPassword) {
                    isLookPassword = false;
                    ivLookPasswords.setImageResource(R.mipmap.icon_look_two);
                } else {
                    isLookPassword = true;
                    ivLookPasswords.setImageResource(R.mipmap.icon_look_one);
                }
                DisplayUtils.setPasswordLookState(isLookPassword, etRegistereAgainPwd);

                break;
        }
    }

    private void register() {
        String affirmPassword = etRegistereAgainPwd.getText().toString().trim();
        String mobileNumber = etRegisterePhone.getText().toString().trim();
        String verificationCode = etRegistereVerificationCode.getText().toString().trim();
//        String email = etRegistereEmail.getText().toString().trim();
//        String userName = etRegistereUserName.getText().toString().trim();
        String nickName  = etRegisterNickName.getText().toString().trim();

        if (TextUtils.isEmpty(mobileNumber)) {
            ToastUtil.show(mContext, "请输入手机号码");
            return;
        }

        if (!RegularUtils.getInstate().rehularPhone(mobileNumber)) {
            ToastUtil.show(mContext, "请输入正确的手机号码");
            return;
        }

        if (TextUtils.isEmpty(verificationCode)) {
            ToastUtil.show(mContext, "请输入短信验证码");
            return;
        }
        if (TextUtils.isEmpty(affirmPassword)) {
            ToastUtil.show(mContext, "请输入密码");
            return;
        }

        if (!RegexpUtils.includeEnglishNumber(affirmPassword)) {
            ToastUtil.show(mContext, "输入的密码包含数字和英文");
            return;
        }

        if (affirmPassword.length() < 6) {
            ToastUtil.show(mContext, "密码长度不能少于6位");
            return;
        }



//        if (TextUtils.isEmpty(userName)) {
//            ToastUtil.show(mContext, "请输入用户名");
//            return;
//        }
        //nickName
        if (TextUtils.isEmpty(nickName)) {
            ToastUtil.show(mContext, "请输入用户昵称");
            return;
        }

//        if (TextUtils.isEmpty(email)) {
//            ToastUtil.show(mContext, "请输入邮箱");
//            return;
//        }
//        if (!RegexpUtils.isEmail(email)) {
//            ToastUtil.show(mContext, "请输入正确的邮箱");
//            return;
//        }

//        if (!ckRegisterSet.isChecked()) {
//            ToastUtil.show(mContext, "请勾选我已阅读并同意 利优国际服务协议》");
//            return;
//        }

        Map<String, Object> mapParameter = new HashMap<>();
//        userName:15207483103
//        password:weixuehu
//        messageId:723873243349
//        imei:18728913219321kj2j13j21jjkjkj
//        verifyCode:435326
        mapParameter.put("messageSn", messageId);    //messageId
        mapParameter.put("verifyCode", verificationCode);          //验证码
        mapParameter.put("password", affirmPassword);//密码，大于5位
        mapParameter.put("userName", mobileNumber);  //电话，唯一
        mapParameter.put("nickName",nickName);
//        mapParameter.put("imei", email);    //邮箱

        showProgressDialog(mContext, "注册中...");
        HttpAsyncTask.getInstance().onPostParam(mContext, "注册中...", false, InterfaceUrl.REGISTE, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                BaseBean bean = (BaseBean) result;
                if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                    ActivityManagerUtils.getInstance().finishAllActivity();
                    ToastUtil.show(mContext, bean.getMessage());
                    finish();
                } else {
                    ToastUtil.show(mContext, bean.getMessage());
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(String message) {
                ToastUtil.show(mContext, message);
                dismissProgressDialog();
            }
        });

    }


    //获取短信验证码
    private void getMessageCode() {
        String mobileNumber = etRegisterePhone.getText().toString().trim();

        if (TextUtils.isEmpty(mobileNumber)) {
            ToastUtil.show(mContext, "请输入手机号码");
            return;
        }

        if (!RegularUtils.getInstate().rehularPhone(mobileNumber)) {
            ToastUtil.show(mContext, "请输入正确的手机号码");
            return;
        }
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put("userName", mobileNumber);
        showProgressDialog(mContext, "验证码发送中...");
        HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.REGISTERCODE, RegisterDataBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    RegisterDataBean bean = (RegisterDataBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        btnVerificationCode.start();
                        messageId = bean.getData();
                        ToastUtil.show(mContext, "验证码发送成功");
                    } else {
                        ToastUtil.show(mContext, bean.getMessage());
                    }
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(String message) {
                ToastUtil.show(mContext, message);
                dismissProgressDialog();
            }
        });

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
